package com.example.simplefileexplorer;


import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class SimpleFileExplorerFragment extends Fragment implements AdapterListener {

    private static final String ARGUMENT_ABSOLUTE_PATH = "ARGUMENT_ABSOLUTE_PATH";

    private RecyclerView recyclerView;
    private TextView pathTextView;
    private SimpleFileExplorerAdapter fileExplorerAdapter;
    private String selectedAbsolutePath;
    private ActivityListener activityListener;

    public static SimpleFileExplorerFragment getInstance(String absolutePath) {
        SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment();
        final Bundle args = new Bundle();
        args.putString(ARGUMENT_ABSOLUTE_PATH, absolutePath);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            this.selectedAbsolutePath = getArguments().getString(ARGUMENT_ABSOLUTE_PATH);
        }
        if (this.selectedAbsolutePath == null) {
            this.selectedAbsolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_simple_file_explorer, container, false);

        this.initViews(view);
        this.loadDirectory();

        this.pathTextView.setText(this.selectedAbsolutePath);

        return view;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    private void initRecyclerView(View view) {
        this.recyclerView = view.findViewById(R.id.recycler_file_explorer);
        this.fileExplorerAdapter = new SimpleFileExplorerAdapter(getContext());
        this.fileExplorerAdapter.setAdapterListener(this);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        this.recyclerView.setHasFixedSize(true);
        this.recyclerView.setAdapter(this.fileExplorerAdapter);
        this.recyclerView.addItemDecoration(new DividerItemDecoration(this.recyclerView.getContext(), DividerItemDecoration.VERTICAL));
    }

    private void loadDirectory() {
        File root = new File(this.selectedAbsolutePath);

        List<FileModel> fileModelList = new ArrayList<>();


        final File[] listFiles = root.listFiles();
        if (root.isDirectory() && listFiles != null) {
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    fileModelList.add(new FileModel(file.getAbsolutePath(), FileModelType.DIRECTORY));
                } else {
                    final File parent = file.getParentFile();
                    if (parent == null) {
                        continue;
                    }
                    fileModelList.add(new FileModel(file.getAbsolutePath(), parent.getAbsolutePath(), FileModelType.FILE));
                }
            }
        } else {
            fileModelList.add(new FileModel(root.getAbsolutePath(), FileModelType.DIRECTORY));
        }

        this.updateRecyclerList(fileModelList);
    }

    private void updateRecyclerList(List<FileModel> fileModels) {
        this.fileExplorerAdapter.loadDirectory(fileModels);
    }


    private void initViews(View view) {
        this.initRecyclerView(view);
        this.pathTextView = view.findViewById(R.id.path_text_view);
    }

    @Override
    public void onDirectoryClick(String selectedAbsolutePath) {
        this.activityListener.onDirectoryChanged(selectedAbsolutePath);
    }

    @Override
    public void onFileClick(FileModel fileModel) {
        this.activityListener.onFileSelect(fileModel);
    }

    void setListeners(ActivityListener activityListener) {
        this.activityListener = activityListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        this.activityListener.onBackButtonPressed(this.selectedAbsolutePath);
    }
}
